﻿using System;
using System.Windows.Forms;
using HDM.NexusEngine.Core;
using HDM.NexusEngine.Input;
using HDM.NexusEngine.PathFinding;
using Tao.OpenGl;

namespace HDM.NexusEngine.Editor
{
    public class EditWalkArea : IGameObject
    {
        private readonly InputManager _input;
        private readonly ToolStrip _toolStrip;
        private readonly StateSystem _editState = new StateSystem();
        private readonly Scene _scene;

        public EditWalkArea(InputManager input, ToolStrip toolstrip, Scene scene)
        {
            _input = input;
            _toolStrip = toolstrip;
            _scene = scene;
            InitEditStates();
            AddToolStripCallbacks();
        }

        public void Update(double elapsedTime)
        {
            _scene.Update(elapsedTime);
            _editState.Update(elapsedTime);
        }

        public void Render()
        {
            GLUtil.Clear(new Color(0.5f, 0.5f, 0.5f, 0));
            Gl.glEnable(Gl.GL_TEXTURE_2D);
            {
                _scene.Render();
            }
            Gl.glDisable(Gl.GL_TEXTURE_2D);
            RenderNavMesh(_scene.NavMesh);

            _editState.Render();
        }

        public void Activated()
        {
            
        }

        private void InitEditStates()
        {
            _editState.AddState("default", new DefaultEditState(_input, _scene.NavMesh));
            _editState.AddState("add_vertex", new AddVertexState(_input, _scene.NavMesh));
            _editState.AddState("add_link", new AddLinkState(_input, _scene.NavMesh));
            _editState.AddState("add_polygon", new AddPolygonState(_input, _scene.NavMesh));
            _editState.ChangeState("default");
        }

        private void AddToolStripCallbacks()
        {
            // Assumes certain buttons are present would be better
            // to add these buttons programmatically here but that makes them a little more bother 
            // to easily edit.
            _toolStrip.Items["_defaultToolStripButton"].Click += OnClickDefaultMode;
            _toolStrip.Items["_addPolygonToolStripButton"].Click += OnClickPolygonAddMode;
            _toolStrip.Items["_linkToolStripButton"].Click += OnClickLinkMode;
            _toolStrip.Items["_addVertexToolStripButton"].Click += OnClickAddVertexMode;
        }

        private void UncheckAllToolstripButtons()
        {
            foreach (ToolStripItem item in _toolStrip.Items)
            {
                var button = item as ToolStripButton;
                if (button != null)
                {
                    button.CheckState = CheckState.Unchecked;
                }
            }
        }

        private void RenderNavMesh(NavMesh navMesh)
        {
            GLUtil.SetColor(new Color(0f, 0f, 1.0f, 0.5f));
            navMesh.PolygonList.ForEach(GLUtil.RenderPolygonFilled);
            GLUtil.SetColor(new Color(0.5f, 0.1f, 0.1f, 1f));
            navMesh.PolygonList.ForEach(GLUtil.RenderPolygon);

        }

        private void OnClickAddVertexMode(object sender, EventArgs e)
        {
            UncheckAllToolstripButtons();
            ((ToolStripButton)_toolStrip.Items["_addVertexToolStripButton"]).CheckState = CheckState.Checked;
            _editState.ChangeState("add_vertex");
        }

        private void OnClickLinkMode(object sender, EventArgs e)
        {
            UncheckAllToolstripButtons();
            ((ToolStripButton)_toolStrip.Items["_linkToolStripButton"]).CheckState = CheckState.Checked;
            _editState.ChangeState("add_link");
        }

        private void OnClickPolygonAddMode(object sender, EventArgs e)
        {
            UncheckAllToolstripButtons();
            ((ToolStripButton)_toolStrip.Items["_addPolygonToolStripButton"]).CheckState = CheckState.Checked;
            _editState.ChangeState("add_polygon");
        }

        private void OnClickDefaultMode(object sender, EventArgs e)
        {
            UncheckAllToolstripButtons();
            ((ToolStripButton)_toolStrip.Items["_defaultToolStripButton"]).CheckState = CheckState.Checked;
            _editState.ChangeState("default");
        }
    }
}